Ext.ns("Ext.baiyue");

var webPath = "/bookshop-admin";
var bookMngMain;
Ext.baiyue.BookMngMain = function(config){
	
	var buttonWidth = 60;
	var gridWidth   = 80;
	//var operationHtml = '<div><span onclick="deleteBook()">删除</span> | <span onclick="modifyBook()">修改</span></div>';
	//工具栏
	var bookmngBar = new Ext.Toolbar({
		items  : [{
			text  : '新增',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				addBook();
			},
		},'|',{
			text  : '入库',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				warehousingBook();
			}
		},'|',{
			text  : '修改',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				modifyBook();
			}
		},'|',{
			text  : '删除',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				var  selection= bookMngInfoGrid.getSelectionModel().getSelection();
				if(selection.length == 0){
					Ext.Msg.alert("温馨提示","没有选择书籍");
					return;
				}else if(selection.length >1){
					Ext.Msg.alert("温馨提示","您的选择多余一行");
					return;
				}
				deleteBook(selection[0].data.bookId);
			}
		}]
	});
	
	//model
	Ext.define("bookInfo",{
		extend 	: 'Ext.data.Model',
		fields 	:[{
			name 	: "bookId",
			type 	: 'string',
		},{
			name : 'bookName',
			type : 'string',
		},{
			name : 'bookType',
			type : 'string',
		},{
			name : 'publisher',
			type : 'string',
		},{
			name : 'publishDate',
			type : 'date',
			dateFormat:'time',
		},{
			name : 'bookSize',
			type : 'int',
		},{
			name : 'edition',
			type : 'string',
		},{
			name : 'author',
			type : 'string',
		},{
			name : 'packStyle',
			type : 'string',
		},{
			name : 'translator',
			type : 'string',
		},{
			name : 'bookCover',
			type : 'string',
		},{
			name : 'bookIsbn',
			type : 'string',
		},{
			name : 'bookPrice',
			type : 'float',
		},{
			name : 'description',
			type : 'string',
		},{
			name : 'catalogue',
			type : 'string',
		},{
			name : 'addTime',
			type : 'date',
			dateFormat:'time',
		},{
			name : 'salePrice',
			type : 'float',
		},{
			name : 'memberPrice',
			type : 'float',
		},{
			name : 'saleCount',
			type : 'int',
		},{
			name : 'bookStock',
			type : 'int',
		}]
	});
	
	//controller
	var bookmngStore = new Ext.data.Store({
		model   : 'bookInfo',
		pageSize: 10,
		proxy   : {
			type : 'ajax',
			url  : "list.do",
			reader: {
				type : 'json',
				root : "root",
			},
			autoLoad:true,
		}
	});
		
	//view
	var bookMngInfoGrid = new Ext.grid.Panel({
		tbar   	:  bookmngBar,
		store  	:  bookmngStore,
		header	: false,
        border	: false,
        draggable: false,
        frame	: false,
        resizable: false,
		region : 'center',
		layout : 'fit',
		width  : '80%',
		
		columns: [{
			header : 'ID',
			width  : gridWidth,
			dataIndex: 'bookId',
		},{
			header : "名称",
			width  : gridWidth,
			dataIndex: 'bookName',
		},{
			header : "分类",
			width  : gridWidth,
			dataIndex: 'bookType',
		},{
			header : "出版商",
			width  : gridWidth,
			dataIndex: 'publisher',
		},{
			header : "出版日期",
			width  : gridWidth,
			dataIndex: 'publishDate',
			renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		},{
			header : "书本大小",
			width  : gridWidth,
			dataIndex: 'bookSize',
		},{
			header : "作者",
			width  : gridWidth,
			dataIndex: 'author',
		},{
			header : "封面类型",
			width  : gridWidth,
			dataIndex: 'packStyle',
		},{		
			header : "译者",
			width  : gridWidth,
			dataIndex: 'translator',
		},{		
			header : "图书封面",
			width  : gridWidth,
			dataIndex: 'bookCover',
		},{		
			header : "条形码",
			width  : gridWidth,
			dataIndex: 'bookIsbn',
		},{		
			header : "图书价格",
			width  : gridWidth,
			dataIndex: 'bookPrice',
		},{		
			header : "图书简介",
			width  : gridWidth,
			dataIndex: 'description',
		},{		
			header : "图书目录",
			width  : gridWidth,
			dataIndex: 'catalogue',
		},{		
			header : "上架时间",
			width  : gridWidth,
			dataIndex: 'addTime',
			renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		},{		
			header : "售价",
			width  : gridWidth,
			dataIndex: 'salePrice',
			
		},{
			header : "会员价",
			width  : gridWidth,
			dataIndex: 'memberPrice',
		},{
			header : "销售次数",
			width  : gridWidth,
			dataIndex: 'saleCount',
		},{
			header : "图书库存",
			width  : gridWidth,
			dataIndex: 'bookStock',
		
		}],
		columnLines : true,
		emptyMsg : "图书",
				
		
	});

	Ext.create('Ext.Viewport', {
			layout : 'border',
			items : [ bookMngInfoGrid ]
	});
	 bookmngStore.load();
	

		this.getStore = function(){
			return bookmngStore;
		}

		return this;
}

//删除书籍动能
	
	function deleteBook(bookId){
		Ext.Msg.confirm("温馨提示",'您确定要删除这本书吗？',function(btn){
			if(btn == 'yes'){
				bookManage('deleteBook.do',{bookId:bookId});
			}
		});
	};

	
	
	var addBook = function(){
		alert("add");
	}
	var modifyBook = function(){
		alert("modify");
	}
	var warehousingBook = function(){
		alert("warehousingBook");
	}

	
	function bookManage(url,params){
		Ext.Ajax.request({
			url 	: url,
			params	: params,
			success:function(resp){
				var tmp = eval('('+resp.responseText+")");
				if(tmp.success){
					Ext.Msg.alert("温馨提示",tmp.message);
					bookMngMain.getStore().reload();
				}else{
					Ext.Msg.alert("温馨提示",tmp.message);
				}
			},
			failure:function(resp){
				Ext.Msg.alert("温馨提示","请求失败");
			}
		});
	}
